import {Component} from '@angular/core';
import {InfiniteScroll, IonicPage, NavController, NavParams} from 'ionic-angular';
import {StorageProvider} from "@providers/storage";
import {PagingConfig} from "@providers/paging";
import {CustomerProvider} from "@providers/interface/customer";
import {DialogProvider} from "@providers/dialog";

/**
 * Generated class for the CustomerOrderListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-customer-order-list',
  templateUrl: 'customer-order-list.html',
})
export class CustomerOrderListPage {

  userInfo: any = this.stroage.read('userInfo');
  isScrollDown = false;//判断页面是否是向下滑动

  //全部订单分页配置
  AllsConfig: PagingConfig = new PagingConfig();
  Alls: any[] = [];

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private stroage: StorageProvider,
              private customer: CustomerProvider,
              private dialog: DialogProvider,) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad CustomerOrderListPage');
  }

  ionViewWillEnter() {
    this.AllsConfig = new PagingConfig();
    this.Alls = [];
    this.selectAll();
  }

  push(paage: string, data?: any) {
    this.navCtrl.push(paage, data)
  }

  /**
   * 查询所有
   */
  selectAll() {
    if (this.Alls.length == 0) {
      this.getData((value) => {
        this.Alls = value;
        console.log(this.Alls);
      }, this.AllsConfig, '2');
    }
  }

  /**
   * 全部订单上滑刷新事件
   */
  getMoreAlls(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      infiniteScroll.complete();
      if (this.AllsConfig.isEnd) {
        infiniteScroll.enable(false);
      }
      this.Alls.push.apply(this.Alls, value);
    }, this.AllsConfig, '2');
  }

  /**
   * 获取数据
   * @param WCZT 状态 -1 未完成   1 完成   2 所有订单
   * @param pagingConfig 分页
   * @param fn 方法
   */
  private getData(fn: (value) => void, pagingConfig, WCZT?: string) {
    if (pagingConfig.isEnd) {
      return;
    }
    this.customer.getMyOrdersList({
      'PARTYID': this.userInfo["PARTYID"],
      'WCZT': WCZT,
      "CXDD": 'CXDD'
    }, pagingConfig)
      .then((data) => {
        fn(data);
        console.info(data);
      }).catch(() => {
    });

  }


  //查看售后
  watchAfterMarket(data: any) {
    if (data.SPLB.length == 1) {
      data = Object.assign(data, data.SPLB[0]);
      this.push('ClassifyPage', data);
    } else if (data.SPLB.length > 1) {
      let type = [];
      data.SPLB.forEach((value, index) => {
        type.push({
          text: `${value.CATEGORY_NAME || ''} ${value.BRANDNAME || ''} ${value.PRODUCT_NAME || ''}`,
          handler: () => {
            data = Object.assign(data, value);
            this.push('ClassifyPage', data);
          }
        })
      });
      this.dialog.showActionSheet(type, '请选择需要申请售后的商品！');
    }
  }

}
